<?php
/**
 * @name /www/notification-widget-ajax.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.03.28
 *
 * Provides ajax functionality for the notification widget
 * in /lib/private/Notification.php.
 */

 // Sets up configuration settings and includes libraries.
require_once('_ini.php');
ReqLib('/lib/private/Notification.php');

/**
 * Sends back ajax data.
 *
 * Because this file provides server-side functionality only,
 * it should only ever need this function.
 */
function DefinePreload() {
  // Provide JSON formatted feedback
  header('content-type: application/json');

  // Complete ajax requests by sending an Action post with the proper value
  switch ($_POST['Action']) {
    case 'Open':
      $id = $_POST['NotificationId'];
      $notification = new Notification();
      $notification->Load($id);
      $notification->IsRead = true;
      $notification->Save();
      $notification->NotificationDate = date('n/j g:iA', $notification->NotificationDate);
      print(json_encode($notification));
      break;

    case 'Delete':
      $id = $_POST['NotificationId'];
      Notification::Delete($id);
      if (!count(Notification::GetNotifications()))
        print(noRecordsMessage());
      break;
  }
  die();
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');